package com.mzj.saas.redis.config;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.repository.service.IOrderLockService;
import com.haixiaoke.saas.repository.service.IOrderOperatingRecordService;
import com.haixiaoke.saas.repository.service.IOrderService;
import com.mzj.saas.redis.listener.RedisKeyExpirationListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * redis监听配置
 *
 * @author zhiping
 */

@Configuration
public class RedisListenerConfig {


    @Autowired
    @Qualifier("stringRedisTemplate")
    private RedisTemplate redisTemplate;

    @Value("${redis.database}")
    private String database;

    @Autowired
    private IOrderService iOrderService;        //订单service
    @Autowired
    private IOrderLockService iOrderLockService;
    @Autowired
    private IOrderOperatingRecordService iOrderOperatingRecordService;    //订单记录service


    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
        String topic = StrUtil.format("__keyevent@{}__:expired", database);
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(new RedisKeyExpirationListener(redisTemplate, database,iOrderService,iOrderLockService,iOrderOperatingRecordService), new PatternTopic(topic));
        // System.out.println("redis监听配置生效");
        return container;
    }

}
